﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using BookManager.Models;

namespace BookManager.Controllers
{

    //[Route("book/add")]
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private static Dictionary<string, AddRequest> DB = new Dictionary<string, AddRequest>();

        // GET api/values
        [HttpGet]
        public ActionResult<IEnumerable<string>> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public ActionResult<string> Get(int id)
        {
            return "value";
        }

        // POST api/values
        [HttpPost]
        public AddResponse Post([FromBody] AddRequest req)
        {
            AddResponse resp = new AddResponse();
            try
            {
                DB.Add(req.ISBN, req);
                resp.ISBN = req.ISBN;
                resp.message = "交易成功";
                resp.result = "s";
            }
            catch (Exception ex)
            {
                resp.ISBN = req.ISBN;
                resp.message = "交易失败";
                resp.result = "F";
                Console.WriteLine(ex);
            }
            return resp;
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
